# Camera Encyclopedia Project Architecture Documentation Generator
Write-Host "Starting to generate Word document..."

try {
    # Create Word application object
    $word = New-Object -ComObject Word.Application
    $word.Visible = $false
    
    # Create new document
    $doc = $word.Documents.Add()
    
    # Set output file path
    $outputFile = "$PSScriptRoot\Camera_Encyclopedia_Architecture.docx"
    
    # Add title
    $titleRange = $doc.Content
    $titleRange.InsertAfter("Camera Encyclopedia Project Architecture")
    $titleRange.Font.Size = 28
    $titleRange.Font.Bold = $true
    $titleRange.ParagraphFormat.Alignment = 1 # Center
    $doc.Content.InsertParagraphAfter()
    
    # Add subtitle
    $subtitleRange = $doc.Content
    $subtitleRange.Collapse(0) # Move to end
    $subtitleRange.InsertAfter("Django 5.2.5 Project")
    $subtitleRange.Font.Size = 18
    $subtitleRange.Font.Bold = $true
    $subtitleRange.ParagraphFormat.Alignment = 1 # Center
    $doc.Content.InsertParagraphAfter()
    
    # Add tech stack
    $techRange = $doc.Content
    $techRange.Collapse(0)
    $techRange.InsertAfter("Tech Stack: Python + Django + SQLite + HTML5 + CSS3")
    $techRange.Font.Size = 14
    $techRange.Font.Bold = $true
    $techRange.ParagraphFormat.Alignment = 1 # Center
    $doc.Content.InsertParagraphAfter()
    
    # Insert page break
    $doc.Content.Collapse(0)
    $doc.Content.InsertBreak(7) # wdPageBreak
    
    # 1. Data Model Section
    $modelTitleRange = $doc.Content
    $modelTitleRange.Collapse(0)
    $modelTitleRange.InsertAfter("1. Data Model Layer")
    $modelTitleRange.Font.Size = 20
    $modelTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    # Camera Category
    $cameraCategoryRange = $doc.Content
    $cameraCategoryRange.Collapse(0)
    $cameraCategoryRange.InsertAfter("1.1 Camera Category")
    $cameraCategoryRange.Font.Size = 16
    $cameraCategoryRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraCategoryTextRange = $doc.Content
    $cameraCategoryTextRange.Collapse(0)
    $cameraCategoryTextRange.InsertAfter("Camera categories for classifying different types of cameras.")
    $doc.Content.InsertParagraphAfter()
    
    # Camera Item
    $cameraItemRange = $doc.Content
    $cameraItemRange.Collapse(0)
    $cameraItemRange.InsertAfter("1.2 Camera Item")
    $cameraItemRange.Font.Size = 16
    $cameraItemRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraItemTextRange = $doc.Content
    $cameraItemTextRange.Collapse(0)
    $cameraItemTextRange.InsertAfter("Camera items contain detailed information such as model, parameters, features, etc.")
    $doc.Content.InsertParagraphAfter()
    
    # Data Relationships Table
    $tableTitleRange = $doc.Content
    $tableTitleRange.Collapse(0)
    $tableTitleRange.InsertAfter("1.5 Data Relationships")
    $tableTitleRange.Font.Size = 16
    $tableTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    # Create table
    $table = $doc.Tables.Add($doc.Content, 4, 2)
    $table.Borders.Enable = $true
    
    # Table header
    $table.Cell(1, 1).Range.Text = "Entity Relationship"
    $table.Cell(1, 2).Range.Text = "Relationship Type"
    $table.Cell(1, 1).Range.Font.Bold = $true
    $table.Cell(1, 2).Range.Font.Bold = $true
    
    # Table content
    $table.Cell(2, 1).Range.Text = "Camera Category - Camera Item"
    $table.Cell(2, 2).Range.Text = "One-to-Many"
    $table.Cell(3, 1).Range.Text = "Accessory Category - Camera Accessory"
    $table.Cell(3, 2).Range.Text = "One-to-Many"
    $table.Cell(4, 1).Range.Text = "Camera Item - Camera Accessory"
    $table.Cell(4, 2).Range.Text = "Many-to-Many (Compatibility)"
    
    $doc.Content.InsertParagraphAfter()
    
    # 2. View Function Section
    $viewTitleRange = $doc.Content
    $viewTitleRange.Collapse(0)
    $viewTitleRange.InsertAfter("2. View Function Layer")
    $viewTitleRange.Font.Size = 20
    $viewTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    # Camera Views
    $cameraViewTitleRange = $doc.Content
    $cameraViewTitleRange.Collapse(0)
    $cameraViewTitleRange.InsertAfter("2.1 Camera Views")
    $cameraViewTitleRange.Font.Size = 16
    $cameraViewTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $cameraViews = @(
        "Camera List - Display all camera items",
        "Camera Detail - Show detailed information of a single camera",
        "Create Camera - Add new camera item",
        "Edit Camera - Modify existing camera information",
        "Delete Camera - Remove camera item"
    )
    
    foreach ($view in $cameraViews) {
        $viewRange = $doc.Content
        $viewRange.Collapse(0)
        $viewRange.InsertAfter($view)
        $viewRange.Font.Bold = $true
        $doc.Content.InsertParagraphAfter()
    }
    
    # 3. Project Features Overview
    $featureTitleRange = $doc.Content
    $featureTitleRange.Collapse(0)
    $featureTitleRange.InsertAfter("3. Project Features Overview")
    $featureTitleRange.Font.Size = 20
    $featureTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $projectFeatures = @(
        "1. Camera Information Display and Management",
        "2. Camera Accessory Display and Management",
        "3. Category Browsing and Filtering",
        "4. Search and Data Statistics",
        "5. Accessory and Camera Compatibility Management"
    )
    
    foreach ($feature in $projectFeatures) {
        $featureRange = $doc.Content
        $featureRange.Collapse(0)
        $featureRange.InsertAfter($feature)
        $featureRange.Font.Bold = $true
        $doc.Content.InsertParagraphAfter()
    }
    
    # 4. Data Statistics
    $statsTitleRange = $doc.Content
    $statsTitleRange.Collapse(0)
    $statsTitleRange.InsertAfter("4. Data Statistics")
    $statsTitleRange.Font.Size = 20
    $statsTitleRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $statsRange = $doc.Content
    $statsRange.Collapse(0)
    $statsRange.InsertAfter("Enhanced Data Statistics")
    $statsRange.Font.Bold = $true
    $doc.Content.InsertParagraphAfter()
    
    $doc.Content.Collapse(0)
    $doc.Content.InsertAfter("Camera Categories: 5 | Camera Items: 13")
    $doc.Content.InsertParagraphAfter()
    
    $doc.Content.Collapse(0)
    $doc.Content.InsertAfter("Accessory Categories: 8 | Accessory Items: 16")
    $doc.Content.InsertParagraphAfter()
    
    # Save document
    $doc.SaveAs([ref]$outputFile)
    $doc.Close()
    $word.Quit()
    
    # Release COM objects
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($table) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
    
    Write-Host "Word document successfully generated: $outputFile"
} catch {
    Write-Host "Error generating Word document: $_"
    # Ensure Word process is released
    if ($word) {
        $word.Quit()
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null
    }
}